package com.dqy.twserver.server.handle.ehandle.mutil;

import com.dqy.twserver.server.dto.GameDto;
import com.dqy.twserver.server.handle.ehandle.GameEventHandle;
import com.dqy.twserver.server.support.Constans;
import com.dqy.twserver.server.support.MsgPush;
import com.dqy.twserver.server.support.Player;
import com.dqy.twserver.server.support.SupportMap;
import com.dqy.twserver.server.support.data.RoomInfo;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class GameEventCreateTerr implements GameEventHandle,IRoom {
    static Logger logger = LoggerFactory.getLogger(GameEventCreateTerr.class);
    @Override
    public void handle(GameDto req, NioSocketChannel channel) throws Exception {
        logger.info("[GameEventCreateTerr] start");

        int rid = roomIdToRid(req.getRoomId());
        RoomInfo roomInfo = SupportMap.roomMap.get(rid);
        String playerName = req.getPlayerName();
        logger.info("[GameEventCreateTerr] dto:{}",req);

        int bulletId = getBulletId(rid, roomInfo);
        req.setInfo(bulletId+"");

        Player player = SupportMap.playerMap.get(playerName);
        int playerColor = player.getPlayerColor();

        //public Bullet(int pid,int num,int x,int y,int v,int dir,int rs)
        StringBuilder sb=new StringBuilder();
        sb.append(playerColor+":");
        sb.append(req.getBulletNum()+":");
        sb.append(req.getPosx()+":");
        sb.append(req.getPosy()+":");
        sb.append(req.getDir()+":");
        sb.append(req.getRandSpeed()+":");
        sb.append(req.getInfo());

        //广播給其他玩家
//        List<Player> collect = roomInfo.getPlayerList().stream().filter(i -> {
//            return ! i.getPlayerName().equals(playerName);
//        }).collect(Collectors.toList());

        // 这里广播給所有人，包括自己
        List<Player> collect = roomInfo.getPlayerList();
        logger.info("[GameEventCreateTerr] sendInfo:{}",sb.toString());
        //将玩家n创建的对象广播給同房间的其他玩家
        MsgPush.sendMsg(collect,sb.toString(),"info", Constans.EVENT_TYPE_CREATE_TERR);

        logger.info("[GameEventCreateTerr] end");
    }

}
